using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SpaceEventManager
{
    public partial class MainGUI : Form
    {
        SpaceManager sManager;
        EventManager eManager;
        PersonManager pManager;
        ResourceManager rManager;
        SpaceManagerGUI sMgrGUI;
        //Dictionary<String, Space> dictSpaces;
        DateTime initial;

        public MainGUI(SpaceManager sMgr, EventManager eMgr, PersonManager pMgr, ResourceManager rMgr, DateTime iInitial)
        {
            sManager = sMgr;
            eManager = eMgr;
            pManager = pMgr;
            rManager = rMgr;
            sMgrGUI = new SpaceManagerGUI(sManager, this);
            InitializeComponent();

            /*
             * set up lstBxSpaces with the list of all spaces
             */
            foreach(Space s in sManager.DictAllSpacesNameKey.Values)
            {
                lstBxSpaces.Items.Add(s.Name);
            }

            /*
             * set the time slider to the current time
             */
            initial = iInitial;
            sldTime.Value = initial.Hour * 12 + initial.Minute/5;
            int hour = sldTime.Value / 12;
            int minute = 5 * (sldTime.Value % 12);
            lblTime.Text = hour + ":" + (minute < 9 ? "0" : "") + minute;
        }

        /*
         * opens the SpaceManagerGUI, passes in this instance of MainGUI
         * for updating purposes. */
        private void btnSpaceGui_Click(object sender, EventArgs e)
        {
            sMgrGUI = new SpaceManagerGUI(sManager, this);
            sMgrGUI.Show();
        }


        /* 
         * When the listbox lstBxSpaces' selection is changed, update the main window's view.
         * Displays selected space in the window, shows its immediate "children" spaces, and
         * displays the space's notes.*/
        private void lstBxSpaces_SelectedIndexChanged(object sender, EventArgs e)
        {
            Space temp = sManager.DictAllSpacesNameKey[lstBxSpaces.SelectedItem.ToString()];
            UpdatePnlView(temp);
        }

        /*
         * internal method that updates the view (pnlView) to display the space passed in.*/
        private void UpdatePnlView(Space iSpace)
        {
            double ratio;
            Label viewSpace = new Label();
            viewSpace.BorderStyle = BorderStyle.FixedSingle;
            viewSpace.AutoSize = false;
            viewSpace.Text = iSpace.Name;
            lblNotes.Text = "Notes for " + iSpace.Name + ": " + iSpace.Notes;

            //This makes sure the entire view panel is used to display the space, without
            //cropping the spaces.
            if (iSpace.Dimensions.X > iSpace.Dimensions.Y)
            {
                viewSpace.Width = pnlView.Width - 50;
                ratio = Convert.ToDouble(viewSpace.Width) / Convert.ToDouble(iSpace.Dimensions.X);
                viewSpace.Height = Convert.ToInt16(iSpace.Dimensions.Y * ratio);
            }
            else
            {
                viewSpace.Height = pnlView.Height - 50;
                ratio = Convert.ToDouble(viewSpace.Height) / Convert.ToDouble(iSpace.Dimensions.Y);
                viewSpace.Width = Convert.ToInt16(iSpace.Dimensions.X * ratio);
            }
            viewSpace.Location = new Point(25, 25);

            pnlView.Controls.Clear();

            //This loops through the space's child spaces, and displays them inside the main space.
            foreach (Space s in iSpace.InnerSpaces.Keys)
            {
                Label inSpace = new Label();
                inSpace.BorderStyle = BorderStyle.FixedSingle;
                inSpace.AutoSize = false;
                inSpace.Text = s.Name;
                inSpace.Width = Convert.ToInt16(s.Dimensions.X * ratio);
                inSpace.Height = Convert.ToInt16(s.Dimensions.Y * ratio);
                inSpace.Left = Convert.ToInt16(25 + (iSpace.InnerSpaces[s].X * ratio));
                inSpace.Top = Convert.ToInt16(25 + (iSpace.InnerSpaces[s].Y * ratio));
                inSpace.DoubleClick += new EventHandler(inSpace_DoubleClick);
                pnlView.Controls.Add(inSpace);
            }

            //Note: must add the child spaces to the panel's controls before the main space, because
            //the panel draws the last control added on top of all the others, and main space would
            //cover the child spaces.
            pnlView.Controls.Add(viewSpace);
            pnlView.Invalidate();
        }

        /*
         * If an innerspace is double clicked, this method changes the main view to that space.*/
        void inSpace_DoubleClick(object sender, EventArgs e)
        {
            string temp = sender.ToString();
            int index = temp.LastIndexOf("Text: ") + 6;
            Space iSpace = sManager.DictAllSpacesNameKey[temp.Substring(index)];
            UpdatePnlView(iSpace);
        }

        /*
         * Takes the slider's value and translates it into 24 hour time, five minute invervals.*/
        private void sldTime_Scroll(object sender, EventArgs e)
        {
            int hour = sldTime.Value/12;
            int minute = 5 * (sldTime.Value % 12);
            
            lblTime.Text = hour + ":" + (minute < 9 ? "0" : "") + minute;
        }

        /*
         * This is called from the managers to update the lists displayed
         * in the main gui's tabs.*/
        public void UpdateLists()
        {
            lstBxSpaces.Items.Clear();
            foreach (Space s in sManager.DictAllSpacesNameKey.Values)
            {
                lstBxSpaces.Items.Add(s.Name);
            }
            
        }

        /*
         * If the search box's text has changed, call a manager's search method, based on
         * what topic is being searched, and what string is being searched. Displays results
         * as a list in the lstSearchResults */
        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            if (rdioSSpaces.Checked)
            {
                List<string> foundSpaces = sManager.SearchAllSpaces(txtSearch.Text);
                lstSearchResults.Items.Clear();
                if (foundSpaces.Count > 0)
                {
                    foreach (string s in foundSpaces)
                    {
                        lstSearchResults.Items.Add(s);
                    }
                }
            }
        }

        /*
         * When one of lstSearchResults' items is clicked, display it on the main view. */
        private void lstSearchResults_DoubleClick(object sender, EventArgs e)
        {
            if (lstSearchResults.SelectedIndex > -1)
            {
                if (rdioSSpaces.Checked)
                {
                    Space view = sManager.DictAllSpacesNameKey[lstSearchResults.SelectedItem.ToString()];
                    UpdatePnlView(view);
                }
            }
        }
        
    }
}